package review.data.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import review.config.Constants;
import review.data.service.JobService;
import review.entity.Job;

@Controller
@RequestMapping("/data")
@Api(tags = { "职务操作接口" })
public class JobAction {
	@Autowired
	private JobService jobService;

	@ApiOperation(value = "添加职务", notes = "", httpMethod = "POST")
	@RequestMapping(value = "/add_job", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object add(Job job) {
		JSONObject obj = new JSONObject();
		try {
			jobService.addJob(job);
			obj.put(Constants.SUCCESS, true);
			obj.put(Constants.DATA, job);
		} catch (Exception e) {
			obj.put(Constants.MESSAGE, e.getMessage());
			obj.put(Constants.SUCCESS, false);
		}
		return obj;
	}

	@ApiOperation(value = "删除职务", notes = "", httpMethod = "POST")
	@RequestMapping(value = "/delect_job", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object delect(Integer[] ids) {
		JSONObject obj = new JSONObject();
		try {
			jobService.deleteJobs(ids);
			obj.put(Constants.SUCCESS, true);
		} catch (Exception e) {
			obj.put(Constants.MESSAGE, e.getMessage());
			obj.put(Constants.SUCCESS, false);
		}
		return obj;
	}

	@ApiOperation(value = "修改职务", notes = "", httpMethod = "POST")
	@RequestMapping(value = "/update_job", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object update(Job job) {
		JSONObject obj = new JSONObject();
		try {
			jobService.updateJob(job);
			obj.put(Constants.SUCCESS, true);
		} catch (Exception e) {
			obj.put(Constants.MESSAGE, e.getMessage());
			obj.put(Constants.SUCCESS, false);
		}
		return obj;
	}

	@ApiOperation(value = "分页查询职务", notes = "", httpMethod = "GET")
	@RequestMapping(value = "/query_job", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object query(Job job, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "1") int size) {
		JSONObject obj = new JSONObject();
		try {
			Page<Job> jobPage = jobService.findAll(page, size, job);
			obj.put(Constants.SUCCESS, true);
			obj.put(Constants.DATA, jobPage);
		} catch (Exception e) {
			obj.put(Constants.MESSAGE, e.getMessage());
			obj.put(Constants.SUCCESS, false);
		}
		return obj;
	}

}
